<?php
namespace Api\Controller;
use Think\Controller;
/**
 * 微信支付
 */
class WeixinpayController extends Controller{

    /**
     * notify_url接收页面
     */
    public function exam_notify(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay = new \Weixinpay();
        $result = $wxpay->notify();
        if ($result) {
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单号
            $out_trade_no = $result['out_trade_no'];
            if ($result['result_code'] == 'SUCCESS') {
                $data['exam_num'] = $out_trade_no;
                    $exam = M('exam')->where($data)->find();
                    if ($exam['exam_paystatus'] == 1) { //如果订单未支付
                        $exam['exam_paystatus'] = 2;    //支付成功
                        $exam['exam_status'] = 1;    //报名成功
                        M('exam')->save($exam);    //保存考试信息
                        //考生推送(暂无)
                        //考生通知
                        $user = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_user_id'])->find();

                        if ($exam['exam_midiclub_id'] != -1) {  //如果是线下考试，增加考场人数
                            $condition['examoffline_roomid'] = $exam['exam_midiclub_id'];
                            $condition['examoffline_ymd'] = $exam['exam_ymd'];
                            $offline = M('examoffline')->where($condition)->find();
                            $major = '';
                            if ($exam['exam_major_id'] == 1) {
                                $offline['examoffline_already_guitar'] += 1;
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $offline['examoffline_already_drum'] += 1;
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $offline['examoffline_already_bass'] += 1;
                                $major = '电贝司';
                            }
                            M('examoffline')->save($offline);
                            //考生通知
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].$major.$exam['exam_level'].'级的线下考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线下报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'];
                            $params_arr['address'] = M('midiclub')->where('midiclub_id = '.$exam['exam_midiclub_id'])->getField('midiclub_address');
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80540036');
                        }else{  //线上考试
                            if ($exam['exam_major_id'] == 1) {
                                $major = '电吉他';
                            }elseif ($exam['exam_major_id'] == 2) {
                                $major = '爵士鼓';
                            }elseif ($exam['exam_major_id'] == 3) {
                                $major = '电贝司';
                            }
                            //考生通知
                            $examtime = M('examtime')->where('examtime_id = '.$exam['exam_time'])->find();
                            $msg_user['message_user_id'] = $exam['exam_user_id'];
                            $msg_user['message_client'] = 1;
                            $msg_user['message_type'] = 2;
                            $msg_user['message_title'] = '尊敬的'.$user['user_name'];
                            $msg_user['message_content'] = '恭喜您成功报名'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级的线上考试,请准时参加！';
                            $msg_user['message_time'] = time();
                            M('message')->add($msg_user);
                            //考生推送
                            $user_content = $msg_user['message_title'].':'.$msg_user['message_content'];
                            $user_value = array('tag'=>array('user'.$exam['exam_user_id']));
                            push($user_value,$user_content,C(JPUSH_USER),2,$exam['exam_id']);
                            //考生线上报名成功短信
                            $params_arr['major'] = $major.$exam['exam_level'].'级';
                            $params_arr['exam_time'] = $exam['exam_ymd'].' '.$examtime['examtime_hi'];
                            $params_json = json_encode($params_arr);
                            alidayumsg($user['user_phone'],$params_json,'SMS_80475033');
                            //考官通知
                            $examiner = M('user')->field('user_name,user_phone')->where('user_id = '.$exam['exam_examiner_id'])->find();
                            $msg_examiner['message_user_id'] = $exam['exam_examiner_id'];
                            $msg_examiner['message_client'] = 2;
                            $msg_examiner['message_type'] = 2;
                            $msg_examiner['message_title'] = '尊敬的'.$examiner['user_name'];
                            $msg_examiner['message_content'] = '您有一场新的考试'.$exam['exam_ymd'].' '.$examtime['examtime_hi'].$major.$exam['exam_level'].'级,请准时监考！';
                            $msg_examiner['message_time'] = time();
                            M('message')->add($msg_examiner);
                            //考官推送
                            $examiner_content = $msg_examiner['message_title'].':'.$msg_examiner['message_content'];
                            $examiner_value = array('tag'=>array('examiner'.$exam['exam_examiner_id']));
                            push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$exam['exam_id']);
                        }
                    }
            }
        }else{
            file_put_contents('a/no.txt', 'no data!');
        }
    }

    public function goods_notify(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay = new \Weixinpay();
        $result = $wxpay->notify();
        if ($result) {
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单号
            $data['order_num'] = $result['out_trade_no'];
            $order_arr = M('order')->where($data)->find();
            $order_arr['order_paystatus'] = 2;
            $order_arr['order_endtime'] = time();
            M('order')->save($order_arr);
        }else{

        }
    }

    public function cert_notify(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay = new \Weixinpay();
        $result = $wxpay->notify();
        if ($result) {
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单号
            $data['certorder_sn'] = $result['out_trade_no'];
            $certorder = M('certorder')->where($data)->find();
            //如果订单未支付，则进行数据库修改
            if ($certorder['certorder_paystatus'] == 1) {
                $certorder['certorder_paystatus'] = 2;
                $certorder['certorder_finishtime'] = time();
                M('certorder')->save($certorder);
            }
        }else{

        }
    }

    public function xxx(){
        echo('asdasd');
    }

    /**
     * 公众号支付 必须以get形式传递 out_trade_no 参数
     * 示例请看 /Application/Home/Controller/IndexController.class.php
     * 中的weixinpay_js方法
     */
    public function pay(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay = new \Weixinpay();
        // 获取jssdk需要用到的数据
        $data = $wxpay->getParameters();
        // 将数据分配到前台页面
        $assign = array(
            'data' => json_encode($data)
            );
        $this->assign($assign);
        $this->display();
    }

    public function test_notify(){
        echo "weixin_test_notify";
    }

    /**
     * notify_url接收页面
     */
    public function examorg_notify(){
        // 导入微信支付sdk
        Vendor('Weixinpay.Weixinpay');
        $wxpay = new \Weixinpay();
        $result = $wxpay->notify();
        if ($result) {
            // 验证成功 修改数据库的订单状态等 $result['out_trade_no']为订单号
            $out_trade_no = $result['out_trade_no'];
            if ($result['result_code'] == 'SUCCESS') {
                $condition['examorg_enroll_sn'] = $out_trade_no;
                $examorg_enroll_arr = M('examorg_enroll')->where($condition)->find();
                //如果订单未支付
                if ($examorg_enroll_arr['examorg_enroll_status'] == 1) {
                    //更改报名状态
                    $examorg_enroll_arr['examorg_enroll_status'] = 2;
                    M('examorg_enroll')->save($examorg_enroll_arr);
                    //机构考试人数+1
                    M('examorg')->where(['examorg_id'=>$examorg_enroll_arr['examorg_enroll_examorgid']])->setInc('examorg_enrollnum',1);
                    //考试专业和考试时段字符串转换
                    $exam_major = '';
                    switch ($examorg_enroll_arr['examorg_enroll_major']) {
                        case '1':
                            $exam_major = '电吉他';
                            break;
                        case '2':
                            $exam_major = '爵士鼓';
                            break;
                        case '3':
                            $exam_major = '电贝司';
                            break;
                        default:
                            $exam_major = '未知专业';
                            break;
                    }
                    $exam_time = '';
                    switch ($examorg_enroll_arr['examorg_enroll_time']) {
                        case '1':
                            $exam_time = '上午';
                            break;
                        case '2':
                            $exam_time = '下午';
                            break;
                        case '3':
                            $exam_time = '全天';
                            break;
                        default:
                            $exam_time = '未知时段';
                            break;
                    }
                    //机构信息
                    $midiclub_arr = M('midiclub')->field('midiclub_address,midiclub_name')->where('midiclub_id = '.$examorg_enroll_arr['examorg_enroll_orgid'])->find();
                    //考生通知（1消息 2推送 3短信）
                    $user_arr = M('user')->field('user_id,user_name,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_userid']])->find();
                    //1消息
                    $condition_stu_msg['message_user_id'] = $user_arr['user_id'];
                    $condition_stu_msg['message_client'] = 1;
                    $condition_stu_msg['message_type'] = 2;
                    $condition_stu_msg['message_title'] = '尊敬的'.$user_arr['user_name'];
                    $condition_stu_msg['message_content'] = '恭喜您成功报名'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级的机构考试,请准时参加！';
                    $condition_stu_msg['message_time'] = time();
                    M('message')->add($condition_stu_msg);
                    //2推送
                    $user_content = $condition_stu_msg['message_title'].':'.$condition_stu_msg['message_content'];
                    $user_value = array('tag'=>array('user'.$user_arr['user_id']));
                    push($user_value,$user_content,C(JPUSH_USER),2,$examorg_enroll_arr['examorg_enroll_id']);
                    //3短信
                    $condition_sms['major'] = $exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级';
                    $condition_sms['exam_time'] = $examorg_enroll_arr['examorg_enroll_date'].$exam_major;
                    $condition_sms['address'] = $midiclub_arr['midiclub_address'];
                    $params_json = json_encode($condition_sms);
                    alidayumsg($user_arr['user_phone'],$params_json,'SMS_80540036');
                    //考官通知（1消息 2推送）
                    $examiner_arr = M('user')->field('user_id,user_name,user_phone')->where(['user_id'=>$examorg_enroll_arr['examorg_enroll_examinerid']])->find();
                    //1消息
                    $condition_examiner_msg['message_user_id'] = $examiner_arr['user_id'];
                    $condition_examiner_msg['message_client'] = 2;
                    $condition_examiner_msg['message_type'] = 2;
                    $condition_examiner_msg['message_title'] = '尊敬的'.$examiner_arr['user_name'];
                    $condition_examiner_msg['message_content'] = $midiclub_arr['midiclub_name'].'有新的考试，'.$examorg_enroll_arr['examorg_enroll_date'].$exam_time.$exam_major.$examorg_enroll_arr['examorg_enroll_level'].'级,请准时监考！';
                    $condition_examiner_msg['message_time'] = time();
                    M('message')->add($condition_examiner_msg);
                    //2推送
                    $examiner_content = $condition_examiner_msg['message_title'].':'.$condition_examiner_msg['message_content'];
                    $examiner_value = array('tag'=>array('examiner'.$examorg_enroll_arr['examorg_enroll_examinerid']));
                    push($examiner_value,$examiner_content,C(JPUSH_EXAMINER),2,$examorg_enroll_arr['examorg_enroll_id']);
                }
            }
        }else{
            file_put_contents('a/no.txt', 'no data!');
        }
    }

}